package freeCellDataStructures;

import java.util.ArrayList;
import java.util.List;
import freeCellBackend.Card;

public class OrderedCascade {
	private List<Card> ocascade;
	
	public OrderedCascade(){
		ocascade = new ArrayList<Card>();
	}
	public OrderedCascade(Card c) {
		ocascade = new ArrayList<Card>();
		this.add(c);
	}
	
	
	public boolean add(Card c){
		if(ocascade.isEmpty()) {
			ocascade.add(c);
			return true;
		} else if((ocascade.get(ocascade.size()-1).getSuite() % 2 != c.getSuite() %2) && 
				 (ocascade.get(ocascade.size()-1).getValue() == c.getValue()+1)){
			ocascade.add(c);
			return true;
		}
		return false;
	}
	public OrderedCascade remove(int quantity){
		OrderedCascade temp = new OrderedCascade();
		int inv = ocascade.size()-quantity;
		if(quantity>=0 && quantity<ocascade.size()){
			for(int j = 0; j < quantity; j++)
				temp.add(ocascade.remove(inv));
			return temp;
		}
		return null;
	}
	public OrderedCascade getOrderedCascade(){
		//To avoid passing direct refERENCE
		OrderedCascade temp = new OrderedCascade();
		for(Card c: ocascade)
			temp.add(c);
		return temp;
	}
	
	public void consolidate(OrderedCascade oc){
		for(int i=0; i<oc.size(); i++)
			this.add(oc.removeBottom());
	}
	public Card getOrderedCascadeCards(int i){
		return ocascade.get(i);
	}
	public Card removeTop(){
		return ocascade.remove(ocascade.size()-1);
	}
	public Card removeBottom(){
		return ocascade.remove(0);
	}
	public Card getTop(){
		return ocascade.get(ocascade.size()-1);
	}
	public Card getBottom(){
		return ocascade.get(0);
	}
	public boolean contains(Card c) {
		for(int i=0;i<ocascade.size();i++) {
			if (ocascade.get(i).equals(c)) {
				return true;
			}
		}
		return false;
	}
	public int size(){
		return ocascade.size();
	}
	public String toString(){
		String temp = "";
		for(Card c : ocascade)
			temp = temp + c.toString();
		return temp;
	}
}